<?php
require_once (__DIR__ . "/../../models/Users.php");
require_once (__DIR__ . "/../../models/Orders.php");
require_once (__DIR__ . "/../../models/Keywords.php");
require_once (__DIR__ . "/../../models/PaymentMethods.php");
require_once (__DIR__ . "/../../models/Addresses.php");

if(isset($_POST['id']))
{
	$row = $_POST;
	$response = Orders::Validate($row);
	if ($response === true)
		$response = Orders::Update($row);
	if($response === true)
		header("Location: $rootUrl/../W/Orders/index");
	
}
else {
	$row = Orders::Get($_REQUEST['id']);
}
?>
				<?php if(isset($response)): ?>
					<dl class="dl-horizontal error">
						<?php foreach ($response as $key => $value): ?>
							<dt><?php echo $key?></dt>
							<dd><?php echo $value?></dd>
						<?php endforeach; ?>						
					</dl>
				<?php endif; ?>
				<form class="form-horizontal" action="" method="post">
					<input type="hidden" name="id" value="<?php echo $row['id'];?>" />
					<div class="control-group">
						<label class="control-label" for="userId">User Id:</label>
						<div class="controls">
<?php $results = Users::GetAll(); 
							echo '<select name="userId">';
	  while($row2 = $results->fetch_assoc()): 
		if ($row2['id']==$row['userId'])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['firstName'].' '.$row2['lastName'].'</option>';					
		else 
				echo '<option value="'.$row2['id'].'" >'.$row2['firstName'].' '.$row2['lastName'].'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="total">Total:</label>
						<div class="controls">
							<input type="number" name="total" id="total" value="<?php echo $row['total']; ?>" 
							class="<?php echo isset($response['total']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['total'])): ?>
									<span class="error"><?php echo $response['total']?></span>
								<?php endif; ?>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="paymentId">Payment Id:</label>
						<div class="controls">
<?php $results = PaymentMethods::GetAll(); 
							echo '<select name="paymentId">';
	  while($row2 = $results->fetch_assoc()): 
		if ($row2['id']==$row['paymentId'])
				echo '<option value="'.$row2['id'].'" selected="selected"> Id: '.$row2['id'].' '.keywords::GetName($row2['accountType']).'</option>';					
		else
				echo '<option value="'.$row2['id'].'"> Id: '.$row2['id'].' '.keywords::GetName($row2['accountType']).'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label" for="dateShipped">Date Shipped:</label>
						<div class="controls">
							<input type="date" name="dateShipped" value="<?php echo $row['dateShipped'];?>" 
class="<?php echo isset($response['dateShipped']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['dateShipped'])): ?>
									<span class="error"><?php echo $response['dateShipped']?></span>
								<?php endif; ?>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="paymentType">Payment Type:</label>
						<div class="controls">
<?php $results = Keywords::GetChildren('paymentType'); 
							echo '<select name="paymentType">';
	  while($row2 = $results->fetch_assoc()): 
		  if ($row2['id']==$row['paymentType'])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['name'].'</option>';					
		  else
				echo '<option value="'.$row2['id'].'" >'.$row2['name'].'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="shipAddress">Ship Address:</label>
						<div class="controls">
<?php $results = Addresses::GetAll(); 
							echo '<select name="shipAddress">';
	  while($row2 = $results->fetch_assoc()): 
		  if ($row2['id']==$row['shipAddress'])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['line_1'].'... </option>';					
		  else
				echo '<option value="'.$row2['id'].'" >'.$row2['line_1'].'... </option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<div class="controls">
							<input type="submit" value="Save" class="btn btn-primary" />
						</div>
					</div>
			
				</form>